package com.yl.paymanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.paymanagement.dao.RechargeDao;
import com.yl.paymanagement.dao.YlFrontAccountDao;
import com.yl.paymanagement.pojo.RechargeRecoding;
import com.yl.paymanagement.pojo.YlFrontAccount;
import com.yl.paymanagement.pojo.YlPayModel;
import com.yl.paymanagement.service.RechargeRecodingService;
import com.yl.paymanagement.utils.GenerateNum;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class RechargeRecodingServiceImpl implements RechargeRecodingService {


    @Autowired
    RechargeDao rechargeDao;

    @Autowired
    YlFrontAccountDao ylFrontAccountDao;

    @Autowired
    RedissonClient redissonClient;


    @Override
    public void add(RechargeRecoding rechargeRecoding) {
        rechargeDao.addByRechargeRecoding(rechargeRecoding);
    }

    @Override
    //@Transactional
    public  void updataStatus(String out_trade_no) {
        RLock myLock = redissonClient.getLock("myLock");
        try {
            myLock.lock();
            RechargeRecoding rechargeRecoding = rechargeDao.findByNum(out_trade_no);
            YlFrontAccount ylFrontAccount = ylFrontAccountDao.findById(rechargeRecoding.getPeopleId());
            BigDecimal total = rechargeRecoding.getRechargeMoney().add(ylFrontAccount.getCardBalance());
            ylFrontAccount.setCardBalance(total);
            ylFrontAccountDao.editBMoney(ylFrontAccount);
            rechargeDao.updataSttusByTradeNo(out_trade_no);
        }catch (Exception exception){
             throw new RuntimeException("存入账户发生异常");
        }
        finally {
            myLock.unlock();
        }




    }

    @Override
    public String create(YlPayModel ylPayModel) {
        try{
            String tradeNo = GenerateNum.generateOrder()+"";
            Date date = new Date();
            RechargeRecoding rechargeRecoding = new RechargeRecoding();
            rechargeRecoding.setPeopleId(ylPayModel.getYlFrontAccount().getAccountId());
            rechargeRecoding.setPayMethod(ylPayModel.getPayType());
            rechargeRecoding.setRechargeMoney(ylPayModel.getTotal_amount());
            rechargeRecoding.setRechargeNum(tradeNo);
            rechargeRecoding.setCardNum(ylPayModel.getYlFrontAccount().getAccountCardNumber());
            rechargeRecoding.setRechargeType(ylPayModel.getSubject());
            rechargeRecoding.setRechargeTime(date);
            rechargeDao.addByRechargeRecoding(rechargeRecoding);
        }
        catch (Exception exception){
            return "false";
        }

        return "ok";
    }

    @Override
    public PageInfo<List<RechargeRecoding>> findListById(Long id,int cunrrentPage) {
        PageHelper.startPage(cunrrentPage,10);
        List<RechargeRecoding> list = rechargeDao.findById(id);
        PageInfo<List<RechargeRecoding>> pageInfo = new PageInfo(list);

        return pageInfo;
    }
}
